import 'package:flutter_account_book/utils/db/db_helper.dart';
import 'package:json_annotation/json_annotation.dart';
part 'record_type.g.dart';

@JsonSerializable()
class RecordType extends DbModel {
  int? id;
  @JsonKey(required: true)
  String name = "";
  // 0内置图标 1文本缩写图标 2应用上传后图标
  @JsonKey(defaultValue: 0)
  int type = 0;
  String? iconPath;
  // 0支出 1收入
  @JsonKey(defaultValue: 0)
  int isIncome = 0;
  int order = 0;
  @JsonKey(defaultValue: 0xFFdc3023)
  int backgroundColorHex = 0xFFdc3023;
  @JsonKey(defaultValue: 0xFFFFFFFF)
  int textColorHex = 0xFFFFFFFF;

  RecordType() : super.fromJson({});

  factory RecordType.fromJson(data) => _$RecordTypeFromJson(data);

  @override
  int get hashCode => id.hashCode;

  @override
  Map<String, dynamic> toJson() => _$RecordTypeToJson(this);
  save() async {
    bool has =
        id == null ? false : await DbHelper.helper.recordTypeDao.has(id!);
    if (!has) {
      await DbHelper.helper.recordTypeDao.insert(this);
    } else {
      await DbHelper.helper.recordTypeDao.update(this);
    }
  }

  @override
  bool operator ==(Object other) {
    return hashCode == other.hashCode;
  }
}
